package com.zou.music_downloader.sqlite;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zou.music_downloader.Constants;
import com.zou.music_downloader.HistoryFragment_;
import com.zou.music_downloader.bean.DataBean;

import java.util.ArrayList;
import java.util.List;


/**
 * 数据库操作类  dao后缀的都是数据库操作类
 * <p>
 * 我们这里的每一个 增删改查 的方法都通过getWritableDatabase()去实例化了一个数据库,这里必须这么做
 * 不客气抽取 成为一个成员变量, 否则报错,若是觉得麻烦可以通过定义方法来置为null和重新赋值
 * <p>
 * —— 其实dao类在这里做得事情特别简单：
 * 1、定义一个构造方法，利用这个方法去实例化一个  数据库帮助类
 * 2、编写dao类的对应的 增删改查 方法。
 */
public class MyDao {

    private MySqliteOpenHelper mMyDBHelper;

    /**
     * dao类需要实例化数据库Help类,只有得到帮助类的对象我们才可以实例化 SQLiteDatabase
     *
     * @param context
     */
    public MyDao(Context context) {
        mMyDBHelper = new MySqliteOpenHelper(context);

    }

    /**
     * DownloadId long primary key,SongId String,DownloadTime long, Path String, SongName String, AuthorName String,State String
     *
     * @param downloadId
     * @param songId
     * @param downloadTime
     * @param path
     * @param songName
     * @param authorName
     * @param state
     * @return
     */
    public long addDate(long downloadId, String songId, long downloadTime, String path, String songName, String authorName, String state) {

        SQLiteDatabase sqLiteDatabase = mMyDBHelper.getWritableDatabase();
        ContentValues  contentValues  = new ContentValues();

        contentValues.put("DownloadId", downloadId);
        contentValues.put("SongId", songId);
        contentValues.put("DownloadTime", downloadTime);
        contentValues.put("Path", path);
        contentValues.put("SongName", songName);
        contentValues.put("AuthorName", authorName);
        contentValues.put("State", state);


        long rowid = sqLiteDatabase.insert(Constants.tableName, null, contentValues);
        sqLiteDatabase.close();

        HistoryFragment_.historyAdapter.notifyDataSetChanged();

        return rowid;
    }


    // 删除的方法，返回值是int
    public int deleteDate(long downloadId) {
        SQLiteDatabase sqLiteDatabase = mMyDBHelper.getWritableDatabase();
        String         arg            = downloadId + "";
        int            deleteResult   = sqLiteDatabase.delete(Constants.tableName, "DownloadId=?", new String[]{arg});
        sqLiteDatabase.close();
        HistoryFragment_.historyAdapter.notifyDataSetChanged();
        return deleteResult;
    }

    public int deleteDateBySongID(String songId) {
        SQLiteDatabase sqLiteDatabase = mMyDBHelper.getWritableDatabase();
        int            deleteResult   = sqLiteDatabase.delete(Constants.tableName, "SongId=?", new String[]{songId});
        sqLiteDatabase.close();
        HistoryFragment_.historyAdapter.notifyDataSetChanged();
        return deleteResult;
    }

    /**
     * 修改的方法
     */
    public int updateData(long downloadId, String state) {
        SQLiteDatabase sqLiteDatabase = mMyDBHelper.getWritableDatabase();
        ContentValues  contentValues  = new ContentValues();
        String         arg            = downloadId + "";
        contentValues.put("State", state);
        int updateResult = sqLiteDatabase.update(Constants.tableName, contentValues, "DownloadId=?", new String[]{arg});
        sqLiteDatabase.close();
        return updateResult;
    }

    /**
     * 查询的方法
     *
     * @param downloadId
     * @return
     */
    public DataBean alterDate(long downloadId) {
        String arg          = downloadId + "";
        String songId;
        long   downloadTime = 0;
        String path;
        String songName;
        String authorName;
        String state;

        DataBean dataBean = null;


        SQLiteDatabase readableDatabase = mMyDBHelper.getReadableDatabase();
        // 查询比较特别,涉及到 cursor
        Cursor cursor = readableDatabase.query(Constants.tableName
                , new String[]{"DownloadId", "SongId", "DownloadTime", "Path", "SongName", "AuthorName", "State"}
                , "DownloadID=?", new String[]{arg}, null, null, null);
        if (cursor.moveToNext()) {
            songId = cursor.getString(1);
            downloadTime = cursor.getLong(2);
            path = cursor.getString(3);
            songName = cursor.getString(4);
            authorName = cursor.getString(5);
            state = cursor.getString(6);

            dataBean = new DataBean(downloadId, songId, downloadTime, path, songName, authorName, state);
        }
        cursor.close(); // 记得关闭 corsor
        readableDatabase.close(); // 关闭数据库
        return dataBean;
    }

    /**
     * 查询的方法
     *
     * @param songId
     * @return
     */
    public DataBean alterDateBySongId(String songId) {
        long   downloadId;
        long   downloadTime = 0;
        String path;
        String songName;
        String authorName;
        String state;

        DataBean dataBean = null;


        SQLiteDatabase readableDatabase = mMyDBHelper.getReadableDatabase();
        // 查询比较特别,涉及到 cursor
        Cursor cursor = readableDatabase.query(Constants.tableName
                , new String[]{"DownloadId", "SongId", "DownloadTime", "Path", "SongName", "AuthorName", "State"}
                , "SongID=?", new String[]{songId}, null, null, null);
        if (cursor.moveToNext()) {
            downloadId = cursor.getLong(0);
            downloadTime = cursor.getLong(2);
            path = cursor.getString(3);
            songName = cursor.getString(4);
            authorName = cursor.getString(5);
            state = cursor.getString(6);

            dataBean = new DataBean(downloadId, songId, downloadTime, path, songName, authorName, state);
        }
        cursor.close(); // 记得关闭 corsor
        readableDatabase.close(); // 关闭数据库
        return dataBean;
    }

    public List<DataBean> getDatas() {
        List<DataBean> datas        = new ArrayList<>();
        long           downloadId   = 0;
        String         songId;
        long           downloadTime = 0;
        String         path;
        String         songName;
        String         authorName;
        String         state;

        DataBean dataBean = null;


        SQLiteDatabase readableDatabase = mMyDBHelper.getReadableDatabase();

        Cursor cursor = readableDatabase.query(Constants.tableName
                , new String[]{"DownloadId", "SongId", "DownloadTime", "Path", "SongName", "AuthorName", "State"}
                , null
                , null, null, null
                , "DownloadTime desc");
        if (cursor.moveToFirst()) {
//            当cursor不是指向最后一行之后，读取所有元素
            while (!cursor.isAfterLast()) {
                downloadId = cursor.getLong(0);
                songId = cursor.getString(1);
                downloadTime = cursor.getLong(2);
                path = cursor.getString(3);
                songName = cursor.getString(4);
                authorName = cursor.getString(5);
                state = cursor.getString(6);

                dataBean = new DataBean(downloadId, songId, downloadTime, path, songName, authorName, state);
                datas.add(dataBean);
                cursor.moveToNext();
            }
        }

//
//        if (cursor.moveToNext()) {
//            downloadId = cursor.getLong(0);
//            songId = cursor.getString(1);
//            downloadId = cursor.getLong(2);
//            path = cursor.getString(3);
//            songName = cursor.getString(4);
//            authorName = cursor.getString(5);
//            state = cursor.getString(6);
//
//            dataBean = new DataBean(downloadId, songId, downloadTime, path, songName, authorName, state);
//        }
        cursor.close(); // 记得关闭 corsor
        readableDatabase.close(); // 关闭数据库
        return datas;
    }


}